package com.bsoft.exchange.service;

import com.bsoft.exchange.dao.his.AppointmentMapper;
import com.bsoft.exchange.dao.his.HospitalizationMapper;
import com.bsoft.exchange.dao.his.SystemConfigMapper;
import com.bsoft.exchange.dao.portal.HospitalInformationMapper;
import com.bsoft.exchange.domain.ElectronicProofDomain;
import com.bsoft.exchange.domain.HospitalInformation;
import com.bsoft.exchange.domain.PatientInfoDomain;
import com.bsoft.exchange.domain.RyZytzDoamin;
import com.bsoft.exchange.pojo.AppointmentCancelPojo;
import com.bsoft.exchange.pojo.HospitalizationAppointmentPojo;
import com.bsoft.exchange.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * 创建日期 : 2019/10/15
 * 创建人 : 赵太行
 * 住院预约
 */
@Service
public class HospitalizationAppointmentService {

    @Autowired
    HospitalizationMapper hospitalizationMapper;
    @Autowired
    PublicService publicService;
    @Autowired
    IdentityManager identityManager;
    @Autowired
    AppointmentMapper appointmentMapper;
    @Autowired
    HospitalInformationMapper hospitalInformationMapper;
    @Autowired
    SystemConfigMapper systemConfigMapper;
    /**
     * 获取住院证信息
     * @param organizationId
     * @param cardType
     * @param cardNumber
     * @return
     */
    public BaseResponse<ElectronicProofDomain> electronicProof(String organizationId,String cardType,String cardNumber){
        BaseResponse<ElectronicProofDomain>dataBaseResponse=new BaseResponse<>();
        try {
        String brid=publicService.getBRID(organizationId,2,cardType,cardNumber,null,null);
        if (TextUtils.isEmpty(brid)){
            dataBaseResponse.setCode(Constant.successResponse);
            dataBaseResponse.setMessage("找不到该病人信息");
            return dataBaseResponse;
        }
        ElectronicProofDomain  electronicProofDomain = hospitalizationMapper.getElectronicProof(organizationId,brid);

        if (electronicProofDomain==null){
            dataBaseResponse.setCode(Constant.successResponse);
            dataBaseResponse.setMessage("未找到该住院证信息");
            return dataBaseResponse;
        }
        if (electronicProofDomain.getCYPB()!=null) {
            if (electronicProofDomain.getCYPB() >= 8) {
                dataBaseResponse.setCode(Constant.successResponse);
                dataBaseResponse.setMessage("该病人已经出院无法获取电子证信息");
                return dataBaseResponse;
            }
        }
        List<HospitalInformation> hospitalInformation=hospitalInformationMapper.selectList(organizationId);
        for (HospitalInformation data:hospitalInformation){
            if (data.getOrganizationId().equals(electronicProofDomain.getOrganizationId())){
                electronicProofDomain.setOrganizationId(electronicProofDomain.getOrganizationId());
                electronicProofDomain.setOrganizationName(data.getOrganizationName());
            }
        }

        dataBaseResponse.setCode(Constant.successResponse);
        dataBaseResponse.setMessage("查询成功");
        dataBaseResponse.setData(electronicProofDomain);
        } catch (SQLException e) {
            e.printStackTrace();
            dataBaseResponse.setCode(Constant.errorTag);
            dataBaseResponse.setMessage("数据库错误");
        }
        return dataBaseResponse;

    }

    /**
     * 预约住院
     * @param pojo
     * @return
     */
    public BaseResponse appointmentSubmit(HospitalizationAppointmentPojo pojo){
        BaseResponse baseResponse=new BaseResponse();
        try {
            if (!"1".equals(pojo.getCardType())) {
                baseResponse.setCode(Constant.errorTag);
                baseResponse.setMessage("只允许身份证查询");
                return baseResponse;
            }else{
                PatientInfoDomain patientInfo = appointmentMapper.getBridSfzh(pojo.getCardNumber());
                if (patientInfo==null){
                baseResponse.setCode(Constant.successResponse);
                baseResponse.setMessage("未找到对应的病人");
                return baseResponse;
            }
                RyZytzDoamin gssb = hospitalizationMapper.selectNotAppointmentCancel(pojo.getOrganizationId(),
                        pojo.getHospitalProveCode());
                if (pojo.getAppointmentType().equals(1)) {
                    //查询该病人是否已经预约过了
                    if (gssb!=null) {
                        baseResponse.setCode(Constant.successResponse);
                        baseResponse.setMessage("请勿重复预约");
                        return baseResponse;
                    }
                   String kdys= systemConfigMapper.getValueByConfigName(Long.parseLong(pojo.getOrganizationId()), "ZSYY_MRGH");
                    Date nlDate=DateUtil.stringToDate(patientInfo.getCSNY());
                    int age=TimeUtil.getAge(nlDate);
                    hospitalizationMapper.appointmentSubmit( patientInfo.getBRID(),
                            patientInfo.getMZHM(), patientInfo.getBRXZ(),
                            patientInfo.getBRXM(), patientInfo.getBRXB(), patientInfo.getCSNY(),kdys,age, pojo);
                }else{
                    //电子
                    if (gssb==null){
                        baseResponse.setCode(Constant.successResponse);
                        baseResponse.setMessage("未查询到电子证信息");
                        return baseResponse;
                    }
                    if (!TextUtils.isEmpty(gssb.getYYZYRQ())){
                        baseResponse.setCode(Constant.successResponse);
                        baseResponse.setMessage("请勿重复预约");
                        return baseResponse;
                    }
                    hospitalizationMapper.updataAppointment(pojo);
                }
                baseResponse.setCode(Constant.successResponse);
                baseResponse.setMessage("预约成功");
            }

        } catch (SQLException e) {
            e.printStackTrace();
            baseResponse.setCode(Constant.errorTag);
            baseResponse.setMessage("查询数据库出错");
            return baseResponse;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baseResponse;
    }
    /**
     * 取消预约
     */
    public BaseResponse appointmentCancel(AppointmentCancelPojo cancelPojo){
        BaseResponse baseResponse=new BaseResponse();
        //获取此次预约是否是已经住院或者取消预约
        try {
            if (!"1".equals(cancelPojo.getCardType())) {
                baseResponse.setCode(Constant.successResponse);
                baseResponse.setMessage("只允许身份证查询");
                return baseResponse;
            } else {

                RyZytzDoamin TZZT=hospitalizationMapper.selectNotAppointmentCancel(cancelPojo.getOrganizationId(),
                        cancelPojo.getHospitalProveCode());
                if (TZZT==null){
                    baseResponse.setCode(Constant.successResponse);
                    baseResponse.setMessage("找不到该住院证信息");
                    return baseResponse;
                }
                if (TZZT.getYYZYRQ()==null){
                    baseResponse.setCode(Constant.successResponse);
                    baseResponse.setMessage("请勿重复取消");
                    return baseResponse;
                }
                if (TZZT.getTZZT()==3||TZZT.getTZZT()==2){
                    baseResponse.setCode(Constant.successResponse);
                    baseResponse.setMessage("该病人已经入院不能取消");
                }else{
                    hospitalizationMapper.appointmentCancel(cancelPojo.getOrganizationId(),cancelPojo.getHospitalProveCode());
                    baseResponse.setCode(Constant.successResponse);
                    baseResponse.setMessage("取消预约成功");
                }
        }
        } catch(SQLException e){
            e.printStackTrace();
            baseResponse.setCode(Constant.errorTag);
            baseResponse.setMessage("数据库异常");
            return baseResponse;
        }
        return baseResponse;
    }
}
